package com.handu.luna.core.domain.promotion;

import javax.persistence.Entity;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * Created by wangzailiang on 2014/6/27.
 */
@Entity
public class PostCardPromotion extends Promotion{

    @NotNull(message = "请输入使用时限")
    @Min(value=0,message = "请输入正确的数字")
    @Max(value=99999999,message = "使用时限最大值为99999999")
    private Long monthLimitCount;
    @NotNull(message = "请输入使用次限")
    @Min(value=0,message = "请输入正确的数字")
    @Max(value=99999999,message = "使用次限最大值为99999999")
    private Long timesLimitCount;

    private Long grantNum = 0L;
    private Long usedNum = 0L;

    /**
     * 修改发放数量
     */
    public void updateNum(Long postCardPromotionId,Long grantNum) {
        PostCardPromotion postCardPromotion = context.getPostCardPromotionRepository().findOne(postCardPromotionId);
        context.getPostCardPromotionRepository().updateGrantNum(postCardPromotion.getGrantNum() + grantNum,postCardPromotionId);
    }

    public PostCardPromotion(){}

    public PostCardPromotion(String name,Long monthLimitCount, Long timesLimitCount, Long grantNum, Long usedNum) {
        this.name = name;
        this.monthLimitCount = monthLimitCount;
        this.timesLimitCount = timesLimitCount;
        this.grantNum = grantNum;
        this.usedNum = usedNum;
    }

    public Long getMonthLimitCount() {
        return monthLimitCount;
    }

    public void setMonthLimitCount(Long monthLimitCount) {
        this.monthLimitCount = monthLimitCount;
    }

    public Long getTimesLimitCount() {
        return timesLimitCount;
    }

    public void setTimesLimitCount(Long timesLimitCount) {
        this.timesLimitCount = timesLimitCount;
    }

    public Long getGrantNum() {
        return grantNum;
    }

    public void setGrantNum(Long grantNum) {
        this.grantNum = grantNum;
    }

    public Long getUsedNum() {
        return usedNum;
    }

    public void setUsedNum(Long usedNum) {
        this.usedNum = usedNum;
    }


    public void updateGrantNum(long grantNum, Long postCardPromotionId) {
        PostCardPromotion postCardPromotion = context.getPostCardPromotionRepository().findOne(postCardPromotionId);
        context.getPostCardPromotionRepository().updateGrantNum(postCardPromotion.getGrantNum() + grantNum,postCardPromotionId);
    }

    public void updateUsedNum(long grantNum, Long postCardPromotionId) {
        PostCardPromotion postCardPromotion = context.getPostCardPromotionRepository().findOne(postCardPromotionId);
        context.getPostCardPromotionRepository().updateUsedNum(postCardPromotion.getUsedNum() + grantNum,postCardPromotionId);
    }
}
